package com.lxw.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lxw.framework.domain.media.MediaFile;
import com.lxw.framework.domain.media.MediaFileProcess_m3u8;
import com.lxw.dao.MediaFileDao;
import com.lxw.framework.utils.HlsVideoUtil;
import com.lxw.framework.utils.Mp4VideoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class PostPageConsumerMq {
    @Autowired
    MediaFileDao mediaFileDao;
    @Value("${yh‐service‐manage‐media.ffmpeg-path}")
    private String ffmpeg_path;
    @Value("${yh‐service‐manage‐media.video-location}")
    private String videoLocation;

    @RabbitListener(queues = "${yh‐service‐manage‐media.mq.queue‐media‐video‐processor}",containerFactory = "customContainerFactory")
    @RabbitHandler
    public void process(String msg){
        // msg ; {"mediaId":"xxxx"}
        if(!msg.startsWith("{")){
            log.error("消费失败{}，格式不正确",msg);
            return;
        }
        JSONObject jsonObject = JSON.parseObject(msg);
        String mediaId = jsonObject.getString("mediaId");
        Optional<MediaFile> byId = mediaFileDao.findById(mediaId);
        if(!byId.isPresent()){
            log.error("消费失败{},文件不存在",msg);
        }

        MediaFile mediaFile = byId.get();
        // 判断媒体文件是否需要处理
        if(mediaFile.getFileType().equals("mp4")){
            // 无需处理
            mediaFile.setProcessStatus("303004");
            mediaFileDao.save(mediaFile);
        }else {
            try {
                mediaFile.setProcessStatus("303001");
                mediaFileDao.save(mediaFile);
                // 重新编码
                String video_path = videoLocation+mediaFile.getFilePath()+mediaFile.getFileName();

                String mp4_name = mediaId+".mp4";

                String mp4_path = videoLocation+mediaFile.getFilePath();
                Mp4VideoUtil videoUtil = new Mp4VideoUtil(ffmpeg_path,video_path,mp4_name,mp4_path);
                String s = videoUtil.generateMp4();
                if(s==null&&!s.equals("success")){
                    // 视频处理失败
                    mediaFile.setProcessStatus("303003");
                    mediaFileDao.save(mediaFile);
                }

                // m3u8生成
                String mp4VideoPath = mp4_path+mp4_name;

                String m3u8_name = mediaId+".m3u8";
                String m3u8_path = videoLocation+mediaFile.getFilePath()+"hls/";
                HlsVideoUtil hlsVideoUtil = new HlsVideoUtil(ffmpeg_path,mp4VideoPath,m3u8_name,m3u8_path);
                String result = hlsVideoUtil.generateM3u8();
                if(result!=null&&result.equals("success")){
                    //
                    mediaFile.setProcessStatus("303002");
                    MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
                    mediaFileProcess_m3u8.setTslist(hlsVideoUtil.get_ts_list());
                    mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
                    mediaFile.setFilePath(mediaFile.getFilePath()+"hls/"+m3u8_name);
                    mediaFileDao.save(mediaFile);
                }else {
                    mediaFile.setProcessStatus("303004");
                    mediaFileDao.save(mediaFile);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
